﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MapAndCharacter_Filip
{
    public class MovableSprite : Sprite
    {

        private Vector2 p_direction;
        private float p_speed = 2.5f;
        private bool p_isMoving;
        private Vector2 p_moveToPosition;

        public MovableSprite(ContentManager c, SpriteBatch s) : base(c, s)
        {
            this.p_direction = Vector2.Zero;
            this.p_isMoving = false;
        }

        // A normalized vector
        public Vector2 Direction
        {
            get { return this.p_direction; }
            set { this.p_direction= value; }
        }

        public void MoveToPosition(Vector2 pos)
        {
            pos.X = pos.X - p_image.Width / 2;
            pos.Y = pos.Y - p_image.Height / 2;
            p_moveToPosition = pos;
            this.Direction = Vector2.Normalize((pos - this.Position));
            this.IsMoving = true;
        }

        public bool IsMoving
        {
            get { return this.p_isMoving; }
            set { this.p_isMoving = value; }
        }

        public void Update(int x, int y)
        {
            
        }

        public void Move()
        {
           // Console.WriteLine("Move Called");
           // Console.WriteLine("( "+this.Position.X+", "+this.Position.Y+") and ("+p_moveToPosition.X+", "+p_moveToPosition+ ")");
            if (IsMoving == false)
                return;
            if (Math.Abs(this.Position.X - p_moveToPosition.X) < 3
                && Math.Abs(this.Position.Y - p_moveToPosition.Y) < 3)
            {
                this.p_isMoving = false;
            }
            else
            {
                this.Position += this.Direction * p_speed;
            }
            
        }
        public override void Draw()
        {
            p_spriteBatch.Draw(this.Image, this.Position, this.Color);
        }

    }

}
